package com.inetools.gwetons.client.impl;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.inetools.gwetons.client.FixPositionInClientWindow.AxisOrigin;

public class CalcFixPositionCoordinateImplIE6 extends CalcFixPositionCoordinateImpl {
	@Override
	public void setPosition( Element element, AxisOrigin axisOrigin, Integer value) {
		
//TODO Need to go up the parent element ladder of element, and find not static position element. What happens if there is fixed position parent element?
		Element parentElement = element.getParentElement();
		
		boolean nonStaticFlag = false;
		while( 
				( parentElement.getNodeType() != Element.DOCUMENT_NODE) && 
				!"BODY".equalsIgnoreCase( parentElement.getTagName())
				) 
		{
			String positionCssStyleRule = parentElement.getStyle().getProperty( "position");
			if ( 
					"relative".equalsIgnoreCase( positionCssStyleRule) || 
					"absolue".equalsIgnoreCase( positionCssStyleRule) || 
					"fixed".equalsIgnoreCase( positionCssStyleRule)
					) 
			{
				nonStaticFlag = true;
				break; // while
			}
			parentElement = parentElement.getParentElement();
		} // while
		
		Style elementStyle = element.getStyle();
		switch( axisOrigin) {
			case Left:
				if ( nonStaticFlag) {
					int parentAbsoluteLeft = element.getParentElement().getAbsoluteLeft();
					elementStyle.setPropertyPx( 
							"left", 
							value + Window.getScrollLeft() - parentAbsoluteLeft
							);
				}
				else {
					elementStyle.setPropertyPx( "left", value + Window.getScrollLeft());
				}
				break;
			case Right:
				if ( nonStaticFlag) {
					parentElement = element.getParentElement();
					int parentAbsoluteLeft = parentElement.getAbsoluteLeft();
					int parentOffsetWidth = parentElement.getOffsetWidth();
					elementStyle.setPropertyPx( 
							"right",
							value + parentAbsoluteLeft + parentOffsetWidth - Window.getScrollLeft() - Window.getClientWidth()
							);
				}
				else {
					elementStyle.setPropertyPx( 
							"right", 
							value + RootPanel.getBodyElement().getOffsetWidth() - Window.getScrollLeft() - Window.getClientWidth());
				}
				break;
			case Top:
				if ( nonStaticFlag) {
					int parentAbsoluteTop = parentElement.getAbsoluteTop();
					elementStyle.setPropertyPx( 
							"top", 
							value + Window.getScrollTop() - parentAbsoluteTop
							);
				}
				else {
					elementStyle.setPropertyPx( "top", value + Window.getScrollTop());
				}
				break;
			case Bottom:
				if ( nonStaticFlag) {
					int parentAbsoluteTop = parentElement.getAbsoluteTop();
					int parentOffsetHeight = parentElement.getOffsetHeight();
					elementStyle.setPropertyPx( 
							"bottom", 
							value + parentAbsoluteTop + parentOffsetHeight - Window.getScrollTop() - Window.getClientHeight()
							);
				}
				else {
					elementStyle.setPropertyPx( 
							"bottom", 
							value + RootPanel.getBodyElement().getOffsetHeight() - Window.getScrollTop() - Window.getClientHeight());
				}
				break;
			default:
				return;
		} // switch
		elementStyle.setProperty( "position", "absolute");
	}
}
